package com.company.project.service.impl;

import com.company.project.core.Page;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.dao.ProfessionalDao;
import com.company.project.model.Professional;
import com.company.project.service.ProfessionalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Description:
 *
 * @author:
 * Date: 2020-04-03
 * Time: 17:10
 */
@Service
public class ProfessionalServiceImpl implements ProfessionalService {
    @Autowired
    ProfessionalDao professionalDao;

    /**
     * 查询所有专业-分页
     *
     * @param college      院系
     * @param professionalName 专业
     * @param pageInfo         分页参数
     * @return
     */
    @Override
    public Result queryProfessionalPage(String college, String professionalName, Page pageInfo) {
        Integer count = professionalDao.countByCondition(college,professionalName);
        pageInfo.setAllRow(count);
        List<Professional> collegeList  = professionalDao.selectByCondition(college,professionalName, pageInfo.getStart(), pageInfo.getSize());
        return ResultGenerator.genSuccessResult(collegeList);
    }

    /**
     * 查询所有专业
     *
     * @return
     */
    @Override
    public Result queryProfessional() {
        List<Professional> professionalList = professionalDao.selectAll();
        return ResultGenerator.genSuccessResult(professionalList);
    }

    /**
     * 查询专业
     *
     * @param id
     * @return
     */
    @Override
    public Result getProfessional(Integer id) {
        Professional professional = professionalDao.selectByPrimaryKey(id);
        return ResultGenerator.genSuccessResult(professional);
    }

    /**
     * 保存专业
     *
     * @param professional
     * @return
     */
    @Override
    public Result saveProfessional(Professional professional) {
        if (professional.getId() == null){
            professionalDao.insert(professional);
        }else{
            professionalDao.updateByPrimaryKey(professional);
        }
        return ResultGenerator.genSuccessResult();
    }

    /**
     * 删除专业
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteProfessional(Integer id) {
        int deleteByPrimaryKey = professionalDao.deleteByPrimaryKey(id);
        if (deleteByPrimaryKey > 0){
            return ResultGenerator.genSuccessResult();
        }else{
            return ResultGenerator.genFailResult();
        }
    }
}
